import { Component, OnInit, ElementRef, Renderer2, OnDestroy } from '@angular/core';
import { NavController, NavParams, ModalController } from '@ionic/angular';
import { DataService } from '@services/data.service';
import { KeyInspectionModal } from '@modals/key-inspection/key-inspection';


@Component({
  selector: 'page-page',
  templateUrl: 'page.html',
  styleUrls: ['page.scss'],
  // providers: [ElementRef, Renderer2]
})

export class KeyinspectionPage implements OnInit, OnDestroy {
  Infosdata;
  processInterval = null;
  scanningWord;
  scrollAreaHeight: any;
  eachTime = 200;
  showNum;
  roomdata;
  scrollInterval = null;
  scrollTimes = 0;
  topInterval = null;
  filePath;
  fileId = null;
  Process = 0;
  alarmData;
  checkedData;

  constructor(
    public el: ElementRef,
    public renderer: Renderer2,
    // public navParams: NavParams,
    public navCtrl: NavController,
    public dataservice: DataService,
    public keyInspectionModal: KeyInspectionModal,
    public modalCtrl: ModalController
  ) {
  }

  ngOnInit() {
    this.getParams();
  }

  ngOnDestroy() {
  }

  goBack() {
    this.navCtrl.pop();
  }

  getParams() {
    this.Infosdata = [];
    this.roomdata = {
      'Title': '百花时代广场一键巡检',
      'AllRoomNum': 1,
      'RoomAbnormalNum': 1,
      'AllDevNum': 18,
      'DevAbnormalNum': 18,
      'Info': [
        {
          'roomID': 1,
          'roomName': '空调机房',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 18,
          'devInfo': [
            {
              'DeviceID': 1,
              'DeviceName': '1#空调主机',
              'realDataInfo': [
                {
                  'PointCode': '1',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '1_1',
                  'PointName': '1#空调主机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
            {
              'DeviceID': 2,
              'DeviceName': '2#空调主机',
              'realDataInfo': [
                {
                  'PointCode': '2',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '2_1',
                  'PointName': '2#空调主机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
            {
              'DeviceID': 3,
              'DeviceName': '3#空调主机',
              'realDataInfo': [
                {
                  'PointCode': '3',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '3_1',
                  'PointName': '3#空调主机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
            {
              'DeviceID': 4,
              'DeviceName': '4#空调主机',
              'realDataInfo': [
                {
                  'PointCode': '4',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '4_1',
                  'PointName': '4#空调主机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
            {
              'DeviceID': 5,
              'DeviceName': '1#冷冻泵',
              'realDataInfo': [
                {
                  'PointCode': '5',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '5_1',
                  'PointName': '1#冷冻泵',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            }
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 2,
          'roomName': '空调机房2',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 3,
          'roomName': '空调机房3',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 1,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 4,
          'roomName': '空调机房4',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 5,
          'roomName': '空调机房5',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 6,
          'roomName': '空调机房6',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 7,
          'roomName': '空调机房7',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        },
        {
          'roomID': 8,
          'roomName': '空调机房8',
          'systemCode': 2,
          'devAllNum_room': 18,
          'devAbnormalNum_room': 0,
          'devInfo': [
            {
              'DeviceID': 15,
              'DeviceName': '1,2,3#冷却风机',
              'realDataInfo': [
                {
                  'PointCode': '15',
                  'DataCode': 1,
                  'CollectTime': '2019-05-07 17:26:33',
                  'CollectData': 0,
                  'DataKey': '15_1',
                  'PointName': '1,2,3#冷却风机',
                  'DataName': '运行状态',
                  'Unit': '',
                  'IsOutRange': false,
                  'IsAlarmInfo': false,
                  'IsCompute': false,
                  'RoundCount': 0,
                  'IsDataEntry': false,
                  'IsStopDisplayParam': true,
                  'Remarks': [
                    {
                      'Key': 0,
                      'Value': '停止'
                    },
                    {
                      'Key': 1,
                      'Value': '运行'
                    }
                  ]
                }
              ],
              'IsBreak': true,
              'abnormalInfo': [

              ],
              'abnormalNum': 0
            },
          ],
          'devAbnormalInfo': [
            '1#空调主机: 通讯中断',
            '2#空调主机: 通讯中断',
            '3#空调主机: 通讯中断',
            '4#空调主机: 通讯中断',
            '1#冷冻泵: 通讯中断',
            '2#冷冻泵: 通讯中断',
            '3#冷冻泵: 通讯中断',
            '4#冷冻泵: 通讯中断',
            '5#冷冻泵: 通讯中断',
            '1#冷却泵: 通讯中断',
            '2#冷却泵: 通讯中断',
            '3#冷却泵: 通讯中断',
            '4#冷却泵: 通讯中断',
            '5#冷却泵: 通讯中断',
            '1,2,3#冷却风机: 通讯中断',
            '4,5,6#冷却风机: 通讯中断',
            '7,8#冷却风机: 通讯中断',
            '9#冷却风机: 通讯中断'
          ]
        }
      ]
    };
    const Infos = this.roomdata.Info;
    // this.checkedData = this.roomdata;
    this.checkedData = JSON.parse(JSON.stringify(this.roomdata));
    this.checkedData.Info = [];
    for (let k = 0; k < Infos.length; k++) {
      for (let j = 0; j < Infos[k].devInfo.length; j++) {
        this.Infosdata.push(Infos[k].roomName + '：' + Infos[k].devInfo[j].DeviceName);
      }
      if (Infos[k].devAbnormalNum_room > 0) {
        this.checkedData.Info.unshift(Infos[k]);
      } else {
        this.checkedData.Info.push(Infos[k]);
      }
    }
    this.startScan();
  }



  startScan() {
    //   // 扫描进度
    this.topScan();

    //   // 进度条进度
    this.processScan();


    this.scroll();
  }


  topScan() {
    this.process(this.eachTime * this.roomdata.AllDevNum);
  }

  processScan() {
    let i = 0;
    const len = this.Infosdata.length;
    const ele = this.el.nativeElement.querySelector('.radar_process');
    this.processInterval = setInterval(() => {
      for (let m = 0; m < this.roomdata.Info.length; m++) {
        if (this.Infosdata[i] === this.roomdata.Info[m].roomName + '：' +
          this.roomdata.Info[m].devInfo[this.roomdata.Info[m].devInfo.length - 1].DeviceName) {
          this.roomdata.Info[m].finished = true;
        }
      }
      this.scanningWord = this.Infosdata[i];
      i++;
      this.renderer.setStyle(ele, 'width', 100 * i / len + '%');
      if (i > len) {
        clearInterval(this.processInterval);
      }
    }, this.eachTime);
  }


  // 巡检信息滚动
  scroll() {
    const el = this.el.nativeElement.querySelector('#scrollArea');
    // this.showNum = parseInt(this.scrollAreaHeight / 49, 10);

    const timePoint = [];       // [ 1000, 1800, 2400]
    let currentData = 0;
    for (let k = 0; k < this.roomdata.Info.length; k++) {
      currentData += this.roomdata.Info[k].devAllNum_room * this.eachTime;
      timePoint.push(currentData);
    }

    let timePointIndex = 0;
    let currentTime = 0;
    this.scrollInterval = setInterval(() => {
      currentTime += this.eachTime;
      if (currentTime >= timePoint[timePointIndex]) {
        this.toTop();
        timePointIndex++;
      }
    }, this.eachTime);

    setTimeout(() => {
      clearInterval(this.scrollInterval);
    }, this.eachTime * this.roomdata.AllDevNum);

  }

  toTop() {
    console.log(this.scrollTimes);
    this.scrollTimes++;
    if (this.scrollTimes >= this.showNum) {
      // const top = $ionicScrollDelegate.$getByHandle('mainScroll').getScrollPosition().top;
      // top += 49;
      // $ionicScrollDelegate.$getByHandle('mainScroll').scrollTo(0, top, true);
    }
  }

  process(T) {
    console.log(T);
    this.topInterval = setInterval(() => {
      if (this.Process < 100) {
        this.Process++;
      } else if (this.Process > 99) {
        clearInterval(this.topInterval);
        // this.showResult();
      }
    }, 0.01 * T);
  }


  // 打开巡检结果模态框

  async showResult() {
    this.alarmData = this.checkedData;
    console.log(this.alarmData);
    const keyModal = await this.modalCtrl.create({
      component: KeyInspectionModal,
      componentProps: { alarmData: this.alarmData }
    });
    keyModal.present();
  }

}
